package com.company.pangu.aliyun.ocr.client;

import com.aliyun.ocr_api20210707.models.RecognizeBankCardResponseBody;
import com.aliyun.ocr_api20210707.models.RecognizeBusinessLicenseResponseBody;
import com.aliyun.tea.TeaException;
import com.company.nuwa.common.utils.JsonUtils;
import com.company.pangu.aliyun.ocr.config.OcrConfig;
import com.company.pangu.aliyun.ocr.request.RecognizeBankCardRequest;
import com.company.pangu.aliyun.ocr.request.RecognizeBusinessLicenseRequest;
import com.company.pangu.aliyun.ocr.request.RecognizeIdcardRequest;
import com.company.pangu.aliyun.ocr.response.RecognizeBankCardResponse;
import com.company.pangu.aliyun.ocr.response.RecognizeBusinessLicenseResponse;
import com.company.pangu.aliyun.ocr.response.RecognizeIdcardResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * <p></p >
 *
 * @author youyoukele
 * @version 1.0
 * @date 2023/4/6 下午5:38
 */
@Slf4j
public class OcrClient {

    private final com.aliyun.ocr_api20210707.Client client;

    public OcrClient(OcrConfig ocrConfig) {
        this.client = createClient(
                ocrConfig.getAccessKeyId(),
                ocrConfig.getAccessKeySecret(),
                ocrConfig.getRegionId()
        );
    }

    /**
     * 身份证识别
     * <p>
     * https://next.api.aliyun.com/api/ocr-api/2021-07-07/RecognizeIdcard?sdkStyle=dara&params={}
     *
     * @param request
     */
    public RecognizeIdcardResponse recognizeIdcard(RecognizeIdcardRequest request) {
        com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest recognizeIdcardRequest = new com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest();
        recognizeIdcardRequest.setOutputFigure(false);
        recognizeIdcardRequest.setOutputQualityInfo(false);
        recognizeIdcardRequest.setUrl(request.getUrl());
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse recognizeIdcardResponse = client.recognizeIdcardWithOptions(recognizeIdcardRequest, runtime);
            log.info("OCR 身份证识别, 请求:{}, 响应:{}", JsonUtils.objectToJson(recognizeIdcardRequest), JsonUtils.objectToJson(recognizeIdcardResponse));
            return JsonUtils.jsonToObject(recognizeIdcardResponse.body.data, RecognizeIdcardResponse.class);
        } catch (TeaException error) {
            log.error(com.aliyun.teautil.Common.assertAsString(error.message));
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            log.error(com.aliyun.teautil.Common.assertAsString(error.message));
        }
        throw new RuntimeException("PICTURE_MISMATCHING");

    }

    /**
     * 银行卡识别请求
     * <p>
     * https://next.api.aliyun.com/api/ocr-api/2021-07-07/RecognizeBankCard?sdkStyle=dara&params={}
     *
     * @param request
     */
    public RecognizeBankCardResponse recognizeBankCard(RecognizeBankCardRequest request) {
        com.aliyun.ocr_api20210707.models.RecognizeBankCardRequest recognizeBankCardRequest = new com.aliyun.ocr_api20210707.models.RecognizeBankCardRequest();
        recognizeBankCardRequest.setUrl(request.getUrl());
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            com.aliyun.ocr_api20210707.models.RecognizeBankCardResponse recognizeBankCardResponse = client.recognizeBankCardWithOptions(recognizeBankCardRequest, runtime);
            log.info("OCR 银行卡识别, 请求:{}, 响应:{}", JsonUtils.objectToJson(recognizeBankCardRequest), JsonUtils.objectToJson(recognizeBankCardResponse));
            if (recognizeBankCardResponse.statusCode == 200) {
                RecognizeBankCardResponseBody body = recognizeBankCardResponse.body;
                return JsonUtils.jsonToObject(body.data, RecognizeBankCardResponse.class);
            }
        } catch (TeaException error) {
            log.error(com.aliyun.teautil.Common.assertAsString(error.message));
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            log.error(com.aliyun.teautil.Common.assertAsString(error.message));
        }
        throw new RuntimeException("PICTURE_MISMATCHING");
    }

    /**
     * 银行卡识别请求
     * <p>
     * https://next.api.aliyun.com/api/ocr-api/2021-07-07/RecognizeBankCard?sdkStyle=dara&params={}
     *
     * @param request
     */
    public RecognizeBusinessLicenseResponse recognizeBusinessLicense(RecognizeBusinessLicenseRequest request) {
        com.aliyun.ocr_api20210707.models.RecognizeBusinessLicenseRequest recognizeBusinessLicenseRequest = new com.aliyun.ocr_api20210707.models.RecognizeBusinessLicenseRequest();
        recognizeBusinessLicenseRequest.setUrl(request.getUrl());
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            com.aliyun.ocr_api20210707.models.RecognizeBusinessLicenseResponse recognizeBusinessLicenseResponse = client.recognizeBusinessLicenseWithOptions(recognizeBusinessLicenseRequest, runtime);
            log.info("OCR 营业执照识别, 请求:{}, 响应:{}", JsonUtils.objectToJson(recognizeBusinessLicenseRequest), JsonUtils.objectToJson(recognizeBusinessLicenseResponse));
            if (recognizeBusinessLicenseResponse.statusCode == 200) {
                RecognizeBusinessLicenseResponseBody body = recognizeBusinessLicenseResponse.body;
                return JsonUtils.jsonToObject(body.data, RecognizeBusinessLicenseResponse.class);
            }
        } catch (TeaException error) {
            log.error(com.aliyun.teautil.Common.assertAsString(error.message));
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            log.error(com.aliyun.teautil.Common.assertAsString(error.message));
        }
        throw new RuntimeException("PICTURE_MISMATCHING");
    }

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    @SneakyThrows
    private static com.aliyun.ocr_api20210707.Client createClient(String accessKeyId, String accessKeySecret, String regionId) {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "ocr-api." + regionId + ".aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }


}
